﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;

namespace Department.admin.Membership
{
    public partial class EditUser : System.Web.UI.Page
    {
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Редакция на потребител";


            if (!IsPostBack)
            {
                string userName = Request.Params["name"];
                
                MembershipUser user = System.Web.Security.Membership.GetUser(userName);
               
                TextBoxUserName.Text = user.UserName;
                TextBoxEmail.Text = user.Email;
                String[] rolesForUser = Roles.GetRolesForUser(userName);
                if (rolesForUser.Length > 0)
                {
                    DropDownListType.SelectedValue = rolesForUser[0];
                }
                DropDownListType.DataSource = Roles.GetAllRoles();
                DropDownListType.DataBind();
            }
        }


        protected void sortByHeading_Click(object sender, CommandEventArgs e)
        {

        }




        protected void itemsView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {



        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
           
            string userName = null;
            string password = null;
            string email = "";
            userName = TextBoxUserName.Text;
            password = TextBoxPassword.Text;
          
            
            try
            {
                email = TextBoxEmail.Text;
            }
            catch (FormatException exc)
            {
                exc.ToString();
                email = "";
            }
            try
            {
                System.Web.Security.Membership.DeleteUser(userName);
                System.Web.Security.Membership.CreateUser(userName, password, email);
            }
            catch (MembershipCreateUserException xc)
            {
                xc.ToString();
            }
            try
            {
                Roles.AddUserToRole(userName, DropDownListType.SelectedValue.ToString());
            }
            catch (Exception ekc)
            {
                ekc.ToString();
            }
            Response.Redirect("ViewAllUsers.aspx");
        }

        

    }
}